package com.lnt.spring;

import com.lnt.spring.bean.AliasBeanTest;
import com.lnt.spring.bean.MyDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @title :
 * @createDate : 2021-10-26 09:42
 * @creater : LuNanTing
 * @className : AppConfig
 */
@ComponentScan("com.lnt.spring")
//@EnableAspectJAutoProxy
@EnableTransactionManagement
@Configuration
//@PropertySource("spring.properties")
public class AppConfig {

    @Bean
    public JdbcTemplate jdbcTemplate(){
        return new JdbcTemplate(dataSource());
    }

    @Bean
    public String userName(){
        return "admin";
    }

    /**
     * 配置事物管理器,并将数据连接池放入管理器
     * 1.在事物管理器中会将连接池一个数据连接conn
     * 2.将数据库连接conn的属性autocommit = false,即自动提交
     * @return
     */
    @Bean
    public PlatformTransactionManager platformTransactionManager(){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource());
        return dataSourceTransactionManager;
    }

//    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/spring-test?userSSL=false&characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("admin3306");
        return dataSource;
    }

//    @Bean
    public MyDataSource myDataSource(){
        return new MyDataSource();
    }

    /**
     * Spring别名Alias测试
     */
    @Bean({"aliasBean1", "aliasBean", "aliasBeanTest", "aliasBeanTest1"})
    public AliasBeanTest aliasBeanTest(){
        System.out.println("Alias Test");
        return new AliasBeanTest();
    }

}
